package org.gl.training.planner.dao;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.gl.training.planner.model.Task;

@Stateless
public class TaskDaoImpl implements TaskDao {

	@PersistenceContext(unitName = "dailyPlannerPU")
	private EntityManager entityManager;

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void delete(Task task) {
		Task deleteTask = entityManager.find(Task.class, task.getId());
		entityManager.remove(deleteTask);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void delete(Integer taskId) {
		Task deleteTask = entityManager.find(Task.class, taskId);
		entityManager.remove(deleteTask);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void save(Task task) {
		entityManager.merge(task);
	}
}
